package com.dreamoo.conf;

import com.dreamoo.interceptor.CorsInterceptor;
import com.dreamoo.interceptor.LoginInterceptor;
import com.dreamoo.redis.RedisCacheUtil;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 山顶洞人郑某
 * @className WebMvcConf.java
 * @date 2025 05 08
 */
@Configuration
@AllArgsConstructor
public class WebMvcConf implements WebMvcConfigurer {

    private RedisCacheUtil redisCacheUtil;
    /**
     * 添加拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new LoginInterceptor(redisCacheUtil))
               .excludePathPatterns("/**").order(-1);
       registry.addInterceptor(new CorsInterceptor()).addPathPatterns("/**").order(-10);
    }

    /**
     * 添加枚举转换器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 注册通用的字符串到枚举的转换器工厂
        registry.addConverterFactory(new StringToEnumConverterFactory());
    }

    /**
     * 设置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加静态资源映射规则
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //配置 knife4j 的静态资源请求映射地址
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
